<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/13
 * Time: 18:53
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Auth;
class ShopOrder extends Model {
	/**
	 * 订单表
	 * @var string
	 */
	protected $table = 'shop_order';

	protected $fillable = [
		'id',
		'uid',
        'freight',
		'openid',
		'ordertype',
		'ordersn',
		'price',
		'originalprice',
		'status',
		'paytype',
		'transid',
		'remark',
		'num',
		'addressid',
		'company_id',
		'is_self',
		'createtime',
		'expressage',
		'courier_number',
		'couriertime',
		'obtaintime',
		'callofftime',
		'calloff',
        'mall_goodsid',
        'mall_data',
        'receipt_name',
        'receipt_adr',
        'receipt_phone',
        'stores_number',
        'is_first',
        'specs',
        'last_credit2s',
        'group_id',
		'snap_type',
		'reward_money',
		'credit1_money',
		'credit2_money',
		'credit5_money',
	];

	/**
	 * 添加订单
	 * @param array $order
	 * @return bool
	 */
	public function add_order($order = array()) {
		if (empty($order)) {
			return false;
		}
		$rico = $this->insertGetId($order);
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}

	/**
	 * 统计订单数量
	 * @param null $uid
	 * @return int
	 */
	public function count_order($uid = null) {
		return $this->where(['uid' => $uid])
            ->where('status','!=',0)
            ->count();
	}

    /**
     * 获取全部订单
     * @param array $keyword
     * @param bool $pag
     * @return bool|\Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
	public function get_order($keyword = array(),$pag = true, $stores = false ) {
	    $table = $this;
	    if ( $stores ) {
	    	$table = $this->where('stores_number','!=','0');
	    } else {
	    	$table = $this->where('stores_number','=','0');
	    }
        //订单类型
        if(!empty($keyword['ordertype'])){
            $table = $this->where(['ordertype' => $keyword['ordertype']]);
        }

        //是否复投
        /*if(!empty($keyword['is_first'])){
            $table = $this->where(['is_first' => $keyword['is_first']]);
        }*/
        //时间查询
        if(!empty($keyword['createtime'])){
            $table = $this->where('createtime','>',strtotime($keyword['createtime'][0]))->where('createtime','<',strtotime($keyword['createtime'][1]));
        }
        //订单状态
        if(isset($keyword['status'])){
            $table = $this->where(['status' => $keyword['status']]);
        }
        //关键字
        if(!empty($keyword['keyword'])){
            $table = $this->where('ordersn','like','%'.$keyword['keyword'].'%')->orWhere('stores_number','like','%'.$keyword['keyword'].'%');
        }
        //关键字搜索出来的会员
        if(!empty($keyword['uid'])){
            $table = $this->whereIn('uid' , $keyword['uid']);
        }
        //当前用户
        $user = Auth::user();
        if($user->is_branch !=0) {
            $table = $table->where('company_id', $user->is_branch);
        }
        if($pag){
            $table = $table->where('status','>',0)->orderBy('paytime','desc');
            $total = $table->sum('price');
            $order['total'] = $total;
            $order['order'] = $table->paginate(15);
        } else {
            $order = $table->orderBy('id','desc')->get()->toArray();
        }

        if ($order) {
            return $order;
        } else {
            return false;
        }
	}


	/**
	 * 查询一条数据
	 * @param null $order_id
	 * @return array|bool
	 */
	public function find_order($order_id = null) {
		$rico = $this->where(['id' => $order_id])->first();
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}


}